---
title: "Experiment 3a"
author: "Jonathan Phillips & Regan Bernhard"
date: "4/30/2022"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r clean_data}
library(tidyverse)

d <- read.csv("exp3a_rawdata.csv")

a <- d

## need to get the list of columns with this in the name: comp3a
compAs <- grep("comp3",names(d))

d <- a %>% select(-c(1:8,10:17,all_of(compAs))) %>% #remove unnecessary columns
  pivot_longer(2:1297, "question","response",values_drop_na=T) %>% #combine all of the results columns
  #filter(bot1==1 & bot2==2) %>%
  separate(question,into = c("scenario","options","orderInfo","question","XX"),sep="_") %>%
  mutate(scenario = as.numeric(gsub("X","",scenario))
         ,altQuality = case_when(
           str_detect(options,"LLH") ~ "Normal Alternative"
           ,str_detect(options,"LHH") ~ "Abnormal Alternative"
         )
         ,questionKind = case_when(
           str_detect(question,"force") ~ "hadTo"
           ,str_detect(question,"norm") ~ "unusualness"
           ,str_detect(question,"val") ~ "goodIdea"
           ,str_detect(question,"comp1") ~ "comprehension1"
           ,str_detect(question,"comp2") ~ "comprehension2"
         )
         ,target = case_when(
            str_detect(question,"actnorm") ~ "actual"
           ,str_detect(question,"actval") ~ "actual"
           ,str_detect(question,"norm1") ~ "alt1"
           ,str_detect(question,"norm2") ~ "alt2"
           ,str_detect(question,"val1") ~ "alt1"
           ,str_detect(question,"val2") ~ "alt2"
         )
         ) %>%
  select(-c(options:XX)) %>%
  pivot_wider(names_from = c(questionKind,target),values_from=value) %>%
  filter(comprehension1_NA==2 & comprehension2_NA==3) %>%
  rowwise() %>% ## Min/Max works here because it just takes the one value for the actual
  mutate(altValue = max(goodIdea_alt1,goodIdea_alt2,na.rm = T)
         ,altNorm = min(unusualness_alt1,unusualness_alt2,na.rm=T)
        ) %>%
  rename(Force=hadTo_NA, actValue=goodIdea_actual, actNorm=unusualness_actual) 

d$actNorm[d$actNorm==Inf] <- NA # one sub didn't give a value for norm resulting in Inf from the Min

#only keep participants with complete data
d <- d %>% drop_na(actNorm)
d <- d %>% drop_na(actValue)

d.all <- d

d <- d.all %>% dplyr::select(-c(age:edu,goodIdea_alt1,goodIdea_alt2,unusualness_alt1,unusualness_alt2))

d$altQuality <- factor(d$altQuality, levels = c("Normal Alternative", "Abnormal Alternative"))

write.csv(d, "exp3a_cleandata.csv")
```

```{r descriptives}
library(plyr)

#get descriptives
force_desc <- ddply(d, c("altQuality"), summarise,
               N    = length(Force),
               mean = mean(Force),
               sd   = sd(Force),
               se   = sd / sqrt(N)
)
print(force_desc)

actval_desc <- ddply(d, "altQuality", summarise,
               N    = length(actValue),
               mean = mean(actValue),
               sd   = sd(actValue),
               se   = sd / sqrt(N)
)
print(actval_desc)

altval_desc <- ddply(d, "altQuality", summarise,
               N    = length(altValue),
               mean = mean(altValue),
               sd   = sd(altValue),
               se   = sd / sqrt(N)
)
print(altval_desc)

actnorm_desc <- ddply(d, c("altQuality"), summarise,
               N    = length(actNorm),
               mean = mean(actNorm),
               sd   = sd(actNorm),
               se   = sd / sqrt(N)
)
print(actnorm_desc)

altnorm_desc <- ddply(d, c("altQuality"), summarise,
               N    = length(altNorm),
               mean = mean(altNorm),
               sd   = sd(altNorm),
               se   = sd / sqrt(N)
)
print(altnorm_desc)

character_desc <- ddply(d, c("altQuality"), summarise,
               N    = length(character_1),
               mean = mean(character_1),
               sd   = sd(character_1),
               se   = sd / sqrt(N)
)
print(character_desc)
```

##Final Analyses
```{r final analyses echo=FALSE}
mosaic::cor.test(altNorm ~ altValue, data = d)

#Predict force judgments by condition. 
summary(lm(Force ~ altQuality, data=d))

#predicting character judgments by whether or not there was a normal alternative
summary(lm(character_1 ~ altQuality, data = d))

#predicting preceptions of the actual actions by condition
summary(lm(actNorm ~ altQuality, data = d))
summary(lm(actValue ~ altQuality, data = d))

#basic models
alt_mod <- (lm(Force ~ altNorm + altValue, data=d))
summary(alt_mod)

act_mod <- (lm(Force ~ actNorm + actValue, data=d))
summary(act_mod)

summary(lm(Force ~ actNorm + altNorm, data = d))
summary(lm(Force ~ actValue + altValue, data = d))

#predict force from just altNorm and just altVal
summary(lm(Force ~ altNorm, data=d))
summary(lm(Force ~ altValue, data=d))

#Are the actual normality judgments in both conditions significantly below 50?
#In other words, are the actual actions always viewed as very normal? 
mosaic::t.test(~actNorm, mu = 50, data = d[d$altQuality == "Normal Alternative",])
mosaic::t.test(~actNorm, mu = 50, data = d[d$altQuality == "Abnormal Alternative",])

#Are the actual value judgments in both conditions significantly above 50?
#In other words, are the actual actions always viewed as very good? 
mosaic::t.test(~actValue, mu = 50, data = d[d$altQuality == "Normal Alternative",])
mosaic::t.test(~actValue, mu = 50, data = d[d$altQuality == "Abnormal Alternative",])

#Are the character judgments in both conditions significantly above 50?
mosaic::t.test(~character_1, mu = 50, data = d[d$altQuality == "Normal Alternative",])
mosaic::t.test(~character_1, mu = 50, data = d[d$altQuality == "Abnormal Alternative",])


#Does judgments of alternatives continue to predict force judgments even when actual actions are viewed as good? 
summary(lm(Force ~ altNorm, data = d[d$actNorm < 50,]))
summary(lm(Force ~ altValue, data = d[d$actValue > 50,]))

```



